var express = require('express');
var Msdb = require("../../../db/Mysqldb.js");
var router = express.Router();   //可使用 express.Router 类创建模块化、可挂载的路由句柄

var db = new Msdb();

router.post("/getOperationLog", async function(req, res) { // 获取系统操作日志
    // 获取数据库连接
    const connection = await db.getConnection();
    var result = {};
    
    try {
        var data = req.body;
        
        // 分页查询操作日志
        const [operationLogs] = await connection.execute(
            `SELECT * FROM operation_log ORDER BY time DESC LIMIT ? OFFSET ?`,
            [data.pageSize, data.offset]
        );
        
        // 查询总记录数
        const [totalResult] = await connection.execute(
            `SELECT COUNT(*) AS total FROM operation_log`
        );
        const total = totalResult[0].total;
        
        result = {
            data: operationLogs,
            total: total
        };
        
        res.send(JSON.stringify(result));
    } catch (error) {
        result = {
            code: 500,
            msg: "服务器内部错误，获取日志失败"
        };
        res.send(JSON.stringify(result));
    } finally {
        // 确保连接释放
        connection.release();
    }
});

module.exports = router;